
***********************************;
** Output hedis2012.sas7bdat      *;
***********************************;

options ls=76 nofmterr;

libname raw '/data/Medicare_P01_2009/data/HEDIS/rawdata';
libname new '/data/Medicare_P01_2009/data/HEDIS/sasdata';


*** Read In Denominator File and CrossWalk file***;

data denom0; set raw.denom2012;

     proc sort; by bene_id; 

data xwalk00; set raw.hic_bene_xwalk_2012;

     if hic_match=1 and sex_match=1;

     length hic_number $ 12;
     hic_number = substr(orig_hic,1,12);

data xwalk0; set xwalk00;

     keep hic_number bene_id;

     proc sort; by bene_id;

data denom12; merge denom0 xwalk0(in=ok); by bene_id;

     if ok;

     denom_flag=1;

     sex  = 1*BENE_SEX_IDENT_CD;
     age  = BENE_AGE_AT_END_REF_YR -1;  /** convert to Age at the Begining to consistent with previous years **/

     length rti_racec gender agec $ 10;

          if rti_race_cd = '1' then rti_racec='1.White';
     else if rti_race_cd = '2' then rti_racec='2.Black';
     else if rti_race_cd = '5' then rti_racec='3.Hispanic';
     else if rti_race_cd = '4' then rti_racec='4.Asian';
     else                           rti_racec='5.Other';

     if rti_racec='1.White'    then white=1;    else white=0;
     if rti_racec='2.Black'    then black=1;    else black=0;
     if rti_racec='3.Hispanic' then hispanic=1; else hispanic=0;
     if rti_racec='4.Asian'    then asian=1;    else asian=0;
     if rti_racec='5.Other'    then othrace=1;  else othrace=0;

     if sex=1 then gender='1.Male';
     if sex=2 then gender='2.Female';

     if age>0   and age<60 then agec='1.<60';
     if age>=60 and age<65 then agec='2.60-64';
     if age>=65 and age<70 then agec='3.65-69';
     if age>=70 and age<75 then agec='4.70-74';
     if age>=75 and age<80 then agec='5.75-79';
     if age>=80 and age<85 then agec='6.80-84';
     if age>=85            then agec='7.85+';
     
     *** Add State and Region Variables ***;

    if state_code='01' then state_ab='AL' ;    if state_code='02' then state_ab='AK' ;
    if state_code='03' then state_ab='AZ' ;    if state_code='04' then state_ab='AR' ;
    if state_code='05' then state_ab='CA' ;    if state_code='06' then state_ab='CO' ;
    if state_code='07' then state_ab='CT' ;    if state_code='08' then state_ab='DE' ;
    if state_code='09' then state_ab='DC' ;    if state_code='10' then state_ab='FL' ;

    if state_code='11' then state_ab='GA' ;    if state_code='12' then state_ab='HI' ;
    if state_code='13' then state_ab='ID' ;    if state_code='14' then state_ab='IL' ;
    if state_code='15' then state_ab='IN' ;    if state_code='16' then state_ab='IA' ;
    if state_code='17' then state_ab='KS' ;    if state_code='18' then state_ab='KY' ;
    if state_code='19' then state_ab='LA' ;    if state_code='20' then state_ab='ME' ;

    if state_code='21' then state_ab='MD' ;    if state_code='22' then state_ab='MA' ;
    if state_code='23' then state_ab='MI' ;    if state_code='24' then state_ab='MN' ;
    if state_code='25' then state_ab='MS' ;    if state_code='26' then state_ab='MO' ;
    if state_code='27' then state_ab='MT' ;    if state_code='28' then state_ab='NE' ;
    if state_code='29' then state_ab='NV' ;    if state_code='30' then state_ab='NH' ;

    if state_code='31' then state_ab='NJ' ;    if state_code='32' then state_ab='NM' ;
    if state_code='33' then state_ab='NY' ;    if state_code='34' then state_ab='NC' ;
    if state_code='35' then state_ab='ND' ;    if state_code='36' then state_ab='OH' ;
    if state_code='37' then state_ab='OK' ;    if state_code='38' then state_ab='OR' ;
    if state_code='39' then state_ab='PA' ;    if state_code='40' then state_ab='PR';

    if state_code='41' then state_ab='RI' ;    if state_code='42' then state_ab='SC' ;
    if state_code='43' then state_ab='SD' ;    if state_code='44' then state_ab='TN' ;
    if state_code='45' then state_ab='TX' ;    if state_code='46' then state_ab='UT' ;
    if state_code='47' then state_ab='VT' ;    if state_code='48' then state_ab='GU';
    if state_code='49' then state_ab='VA' ;    if state_code='50' then state_ab='WA' ;

    if state_code='51' then state_ab='WV' ;    if state_code='52' then state_ab='WI' ;
    if state_code='53' then state_ab='WY' ;    

    length region9 region4 $ 25; 

    if state_ab in ('CT','MA','ME','NH','RI','VT')                then region9='1.New England';
    if state_ab in ('NJ','NY','PA')                               then region9='2.Mid Atlantic'; 
    if state_ab in ('WI','MI','IL','IN','OH')                     then region9='3.East North Central'; 
    if state_ab in ('ND','SD','NE','KS','MN','IA','MO')           then region9='4.West North Central'; 
    if state_ab in ('DE','MD','DC','FL','GA','NC','SC','VA','WV') then region9='5.South Atlantic';
    if state_ab in ('KY','TN','MS','AL')                          then region9='6.East South Central'; 
    if state_ab in ('OK','AR','TX','LA')                          then region9='7.West South Central';
    if state_ab in ('MT','ID','WY','NV','UT','CO','AZ','NM')      then region9='8.Mountain'; 
    if state_ab in ('WA','OR','CA','HI','AK')                     then region9='9.Pacific';

    if region9='1.New England' or 
       region9='2.Mid Atlantic'        then region4='Region 1: Northeast';
    if region9='3.East North Central' or 
       region9='4.West North Central'  then region4='Region 2: Midwest';
    if region9='5.South Atlantic' or 
       region9='6.East South Central' or 
       region9='7.West South Central' then region4='Region 3: South';
    if region9='8.Mountain' or 
       region9='9.Pacific'             then region4='Region 4: West';

     zip5=1*substr(bene_zip_cd,1,5);  ** will add zip info when analysing **;

     proc sort nodupkey; by hic_number;
     proc sort nodupkey; by bene_id;  /* questionable links will be removed - should be small if any <0.1% **/
     
     proc sort; by zip5;

data zip; set new.zipinfo;

     proc sort; by zip5;

data raw.denom2012hedis; merge denom12(in=ok) zip; by zip5;

     if ok;

endsas;